package util;

import android.os.Handler;
import android.os.Message;
import android.view.View;

/**
 * @author xiaguojin
 * @time 2020-05-21 11:09
 * @change
 * @chang time
 * @class 隐藏 显示view
 */
public class HideControl {
    public final static int MSG_HIDE = 0x01;

    private HideHandler mHideHandler;
    View hideView;
    long time;

    public HideControl() {
        mHideHandler = new HideHandler();
    }

    public class HideHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_HIDE:
                    if (hideView!=null){
                        hideView.setVisibility(View.GONE);
                        endHideTimer();
                    }
                    break;
            }

        }
    }

    private Runnable hideRunable = new Runnable() {

        @Override
        public void run() {
            mHideHandler.obtainMessage(MSG_HIDE).sendToTarget();
        }
    };

    public void startHideTimer(View view,long time) {//开始计时,三秒后执行runable
        mHideHandler.removeCallbacks(hideRunable);
        this.hideView = view;
        this.time = time;
        if(hideView!=null&&hideView.getVisibility() == View.GONE){
            hideView.setVisibility(View.VISIBLE);
        }
        mHideHandler.postDelayed(hideRunable, time);
    }

    public void endHideTimer() {//移除runable,将不再计时
        mHideHandler.removeCallbacks(hideRunable);
    }

    public void resetHideTimer() {//重置计时
        mHideHandler.removeCallbacks(hideRunable);
        mHideHandler.postDelayed(hideRunable,time);
    }

}
